package com.tenement.controller;

import com.alibaba.fastjson.JSONObject;
import com.mybatisflex.core.paginate.Page;
import com.tenement.common.Axios;
import com.tenement.entity.Repair;
import com.tenement.enums.RespBusiness;
import com.tenement.service.RepairService;
import com.tenement.vo.QueryVo;
import com.tenement.vo.RepairVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author BertXie
 * @since 1.0.1
 */
@RestController
@RequestMapping("/repair")
@RequiredArgsConstructor
@Api(tags = "报修管理接口")
public class RepairController {

    private final RepairService repairService;


    @ApiOperation(value = "操作报修数据")
    @PutMapping("operate")
    public Axios operate(@RequestBody Repair repair) {
       return repairService.getMapper().insertOrUpdateSelective(repair) > 0 ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation(value = "获取报修列表")
    @PreAuthorize("@exp.hasAuthority('tenement:repair:list')")
    @PostMapping("list")
    public Axios list(@RequestBody QueryVo queryVo) {
        Page<Repair> page = repairService.list(queryVo);
        return Axios.success(page);
    }

    @ApiOperation(value = "删除报修数据")
    @DeleteMapping("remove")
    public Axios remove(@RequestBody List<Serializable> ids) {
        return repairService.getMapper().deleteBatchByIds(ids) > 0 ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation(value = "分配维修师傅")
    @PostMapping("assign")
    public Axios assign(@RequestBody RepairVo repairVo) {
        return repairService.assign(repairVo) ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("改变维修单状态")
    @PostMapping("changeStatus")
    public Axios changeStatus(@RequestBody Repair repair) {
        return repairService.changeStatus(repair) ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

}
